<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24"> </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button @click="searchReset" type="primary" icon="plus">重置</a-button>
      <!-- 高级查询区域 -->
      <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :customRow="clickThenSelect"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="getCompensateRecord(record.id)" v-if="record.isGenerated === 1">查看补差记录</a>
          <a @click="generateCompensateRecord(record)" v-else>生成补差记录</a>
          <a-divider type="vertical" />
          <a @click="handleDelete(record.id)">删除</a>
        </span>
      </a-table>
    </div>

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="采购补差明细" key="1">
        <HfPurchaseCompensatePriceDetailList :mainId="selectedMainId" />
      </a-tab-pane>
    </a-tabs>

    <hfPurchaseCompensatePrice-modal ref="modalForm" @ok="modalFormOk"></hfPurchaseCompensatePrice-modal>
    <HfPurchaseCompensateRecordList
      :mainId="selectedMainId"
      @close="searchReset"
      ref="HfPurchaseCompensateRecordList"
    />
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfPurchaseCompensatePriceModal from './modules/HfPurchaseCompensatePriceModal'
import { getAction, postAction } from '@/api/manage'
import HfPurchaseCompensatePriceDetailList from './HfPurchaseCompensatePriceDetailList'
import '@/assets/less/TableExpand.less'
import HfPurchaseCompensateRecordList from '@views/purchase/table/HfPurchaseCompensateRecordList.vue'
import HfSaleCompensateRecordList from '@views/sale/table/HfSaleCompensateRecordList.vue'

export default {
  name: 'HfPurchaseCompensatePriceList',
  mixins: [JeecgListMixin],
  components: {
    HfSaleCompensateRecordList,
    HfPurchaseCompensateRecordList,
    HfPurchaseCompensatePriceDetailList,
    HfPurchaseCompensatePriceModal
  },
  data() {
    return {
      description: '采购补差管理页面',

      // 表头
      columns: [
        {
          title: '方案名',
          align: 'center',
          dataIndex: 'projectName'
        },
        {
          title: '开始日期',
          align: 'center',
          dataIndex: 'startDate'
        },
        {
          title: '结束日期',
          align: 'center',
          dataIndex: 'endDate'
        },
        {
          title: '制单',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/purchase/hfPurchaseCompensatePrice/list',
        delete: '/purchase/hfPurchaseCompensatePrice/delete',
        deleteBatch: '/purchase/hfPurchaseCompensatePrice/deleteBatch',
        exportXlsUrl: '/purchase/hfPurchaseCompensatePrice/exportXls',
        importExcelUrl: 'purchase/hfPurchaseCompensatePrice/importExcel',
        generateCompensateRecord: 'purchase/hfPurchaseCompensatePrice/generateCompensateRecord',
        getCompensateRecord: 'purchase/hfPurchaseCompensatePrice/getCompensateRecord'
      },
      dictOptions: {},
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['5', '10', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    initDictConfig() {},
    getCompensateRecord() {
      this.$refs.compensateRecordList.visible = true
    },
    generateCompensateRecord(record) {
      postAction(this.url.generateCompensateRecord, record).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params).then(res => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'projectName', text: '方案名', dictCode: '' })
      fieldList.push({ type: 'string', value: 'operator', text: '制单', dictCode: '' })
      fieldList.push({ type: 'Text', value: 'remark', text: '备注', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
